<template>
	<view class="content">
		<!-- <nav-header /> -->
		<view class="tab-card flex-row-start" :style="{top: `${getTop()}`}">
			<view class="tab-item" :class="{'tab-item-active': tabId == 1}" @click="setTabId(1)">未完成</view>
			<view class="tab-item" :class="{'tab-item-active': tabId == 2}" @click="setTabId(2)">已完成</view>
		</view>
		<view class="list-card-wrap" style="margin-top: 96rpx;">
			<view v-for="(item,index) in tableData" :key="index" class="list-card" @click="toPage(item)">
				<view class="flex-row-between">
					<view @click.stop="toDetail(item)" class="flex-row">
						<text class="gray-text">任务编号</text>
						<text class="black-text-34" style="margin-left: 20rpx;">{{item.no}}</text>
						<uni-icons type="right" size="16" color="#8997A8"></uni-icons>
					</view>
					<view class="tag">{{item.storehouseLocationNo || item.storehouseNo}}</view>
				</view>
				<view class="flex-row-between">
					<view class="flex-row">
						<text class="gray-text font-34">{{item.productName}}</text>
						<text class="black-text-34" style="margin-left: 20rpx;">({{item.productNo}})</text>
					</view>
					<!-- <view class="tag">{{item.area}}</view> -->
				</view>
				<view class="content flex-row">
					<!-- <view class="flex-column flex1">
						<text class="gray-text">物料名称</text>
						<text class="black-text">{{item.productName}}</text>
					</view> -->
					<view v-if="queryParams.orderType<=3" class="flex-column flex1">
						<text class="gray-text">区域</text>
						<text class="black-text">{{item.area}}</text>
					</view>
					<view v-if="queryParams.orderType<=3" class="split"></view>
					<view class="flex-column flex1">
						<text class="gray-text">入库申请数量</text>
						<text class="black-text">{{item.orderQty}}</text>
					</view>
					<view class="split"></view>
					<view class="flex-column flex1">
						<text class="gray-text">实际入库数量</text>
						<text class="blue-text">{{item.actualQty}}</text>
					</view>
				</view>
				<view v-if="tabId == 1" class="flex-row-between">
					<text class="gray-text">创建时间 {{item.createTime}}</text>
					<view v-if="item.actualQty==0" class="flex-row">
						<navigator :url="'/pages/enter/production/edit?id='+item.id+'&no='+item.no" hover-class="other-navigator-hover">
							<view class="botton p-botton">码货</view>
						</navigator>
					</view>
					<view v-else class="flex-row">
						<navigator :url="'/pages/enter/production/edit?id='+item.id+'&no='+item.no" hover-class="other-navigator-hover">
							<view class="botton">码货</view>
						</navigator>
						<view @click="complete(item)" class="botton p-botton">完成</view>
					</view>
				</view>
				<view v-if="tabId == 2" class="">
					<view class="flex-row-between">
						<text class="gray-text">创建时间</text>
						<text class="gray-text">{{item.createTime}}</text>
					</view>
					<view class="flex-row-between">
						<text class="gray-text">完成时间</text>
						<text class="gray-text">{{item.completeTime}}</text>
					</view>
				</view>
			</view>
			<list-footer :total="total" :dataLength="tableData.length"></list-footer>
		</view>
	</view>
</template>

<script>
	import {
		getYardageGoodsTaskOrderList,
		getInStockBatchInfo,
		inoutstock,
		complete
	} from "./api"
	import { getTop } from '@/utils/common.js'
	var printModule = uni.requireNativePlugin("pda-print-PdaPrintModule")
	export default {
		data() {
			return {
				getTop,
				// 遮罩层
				loading: true,
				// 查询参数
				queryParams: {
					shipper:this.$store.state.user.name,
					pageNum: 1,
					pageSize: 10,
					orderType:'',
					orderStatusList:['1','2']
				},
				total: 0,
				searchValue: '',
				tabId: 1,
				tableData: [],
				// form: {
				// 	batchNo: '',
				// 	storehouseLocationNo: '',
				// 	productNo: '',
				// 	productName: '',
				// 	quantity: ''
				// }
			}
		},
		onNavigationBarSearchInputChanged(obj){
			if(!obj.text){
				this.queryParams.no = '';
				this.refreshList()
			}
		},
		onNavigationBarSearchInputConfirmed(obj) {
			// obj.text
			this.queryParams.no = obj.text;
			this.refreshList()
		},
		// 上拉加载
		onReachBottom() {
			if (this.tableData.length == this.total) {
				return;
			}
			this.queryParams.pageNum++;
			this.getList();
		},
		onLoad(option) {
			let that = this
			uni.$on('refreshList',function(data){
				that.refreshList()
			})
			// 入库：1生产入库 2退料入库 3报废入库 4退货入库 5待检入库 6收货入库 7调拨入库 8其他入库
			this.queryParams.orderType = option.orderType
			this.getList()
		},
		onUnload(){
			uni.$off('refreshList')
		},
		methods: {
			toPage(item) {
				if (this.tabId == 2) {
					this.toDetail(item)
				}
			},
			toDetail(item){
				this.$tab.navigateTo("/pages/enter/production/detail?id="+item.id);
			},
			setTabId(tabId) {
				this.tabId = tabId;
				this.refreshList();
			},
			clearForm() {
				this.form = {
					batchNo: '',
					storehouseLocationNo: '',
					productNo: '',
					productName: '',
					quantity: ''
				}
			},
			getInStockBatchInfo(e) {
				if (e.target.value === "") {
					this.$modal.msg("请输入批次号")
					this.clearForm()
				} else {
					getInStockBatchInfo(e.target.value).then(res => {
						if (res.code == 200) {
							this.form = res.data
						}
					})
				}
			},
			refreshList(){
				this.tableData = [];
				this.total = 0;
				this.queryParams.pageNum = 1;
				this.getList();
			},
			getList() {
				uni.showLoading({
					title: '加载中...'
				});
				if(this.tabId == 2){
					this.queryParams.orderStatusList = ['3']
				}else{
					this.queryParams.orderStatusList = ['1','4']
				}
				getYardageGoodsTaskOrderList(this.queryParams).then(res => {
					if (res.code == 200) {
						this.tableData = this.tableData.concat(res.rows);
						this.total = res.total;
					}
					uni.hideLoading();
				});
			},
			complete(item){
				if(item.actualQty && item.actualQty!=0){
					// [${item.no}]
					this.$modal.confirm(`是否确认完成任务`).then(() => {
						complete(item).then(res=>{
							this.refreshList()
							this.$modal.msgSuccess("操作成功");
						})
					})
				}else{
					// [${item.no}]
					this.$modal.msg(`任务单实际入库数量不能为0`);
					return
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F8F8F8;
	}
</style>
<style lang="scss" scoped>
	.content {
		// margin: 17rpx 25rpx;
	}
</style>
